package org.example.ffmmx.bmapandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.baidu.mapapi.map.*;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.*;

import java.util.ArrayList;
import java.util.List;

public class MarkerPoiDemo extends Activity {
    TextureMapView bMapView = null;
    int markerCount = 1;
    BaiduMap map;
    List<OverlayOptions> needRenderOverlays;
    public MarkerPoiDemo() {
        super();
        needRenderOverlays=new ArrayList<>();
    }

    class MarkerPopClickListener implements View.OnClickListener {
        private Marker marker;

        public MarkerPopClickListener(Marker marker) {
            this.marker = marker;
        }

        @Override
        public void onClick(View v) {
            if(R.id.markerInfoWEnterBtn == v.getId()){

            }
            else if (R.id.markerInfoWCancelBtn ==v.getId()) {
                for (OverlayOptions renderOverlay : needRenderOverlays) {
                    if(renderOverlay instanceof MarkerOptions){
                        LatLng saveMarkerPoint=((MarkerOptions) renderOverlay).getPosition();
                        if(saveMarkerPoint.latitude == marker.getPosition().latitude
                                && saveMarkerPoint.longitude == marker.getPosition().longitude){
                            needRenderOverlays.remove(renderOverlay);
                        }
                    }
                }

                map.hideInfoWindow();
                map.clear();

            }
        }
    }
    class MarkerPoiClickHandler implements BaiduMap.OnMarkerClickListener {
        @Override
        public boolean onMarkerClick(Marker marker) {
            //创建InfoWindow展示的view
            View markerPopView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.marker_pop, null);
            Button markerBtn = (Button) markerPopView.findViewById(R.id.markerInfoWEnterBtn);
            Button cancelBtn = (Button) markerPopView.findViewById(R.id.markerInfoWCancelBtn);
            MarkerPopClickListener markerPopClickListener=new MarkerPopClickListener(marker);
            markerBtn.setOnClickListener(markerPopClickListener);
            cancelBtn.setOnClickListener(markerPopClickListener);

//定义用于显示该InfoWindow的坐标点
//创建InfoWindow , 传入 view， 地理坐标， y 轴偏移量
            InfoWindow mInfoWindow = new InfoWindow(markerPopView, marker.getPosition(), -60);
//显示InfoWindow
            map.showInfoWindow(mInfoWindow);

            return false;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marker_demo);

        bMapView = (TextureMapView) findViewById(R.id.bmapView);
        bMapView.showScaleControl(false);
        bMapView.showZoomControls(true);
        bMapView.removeViewAt(1);  //去掉百度LOGO

        map = bMapView.getMap();
        map.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        map.setMyLocationEnabled(true);
        map.setOnMarkerClickListener(new MarkerPoiClickHandler());
        map.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
//                取经纬度
                double latitude = latLng.latitude;
                double longitude = latLng.longitude;
                System.out.println("latitude=" + latitude + ",longitude=" + longitude);

//                清楚图层
                map.clear();
//                定义标记坐标点
                LatLng point = new LatLng(latitude, longitude);
//                构建标记点配置项，用于在地图上添加标记
                View markerView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.marker, null);
                TextView markerTitle = (TextView) markerView.findViewById(R.id.markerTitle);
                markerTitle.setText(markerCount + "");
                markerCount++;
                BitmapDescriptor markIcon1 = BitmapDescriptorFactory.fromView(markerView);
                BitmapDescriptor markIcon2 = BitmapDescriptorFactory.fromView(markerView);
                MarkerOptions options = new MarkerOptions().position(point).icon(markIcon1);

//                在地图上添加标记，并且显示
                map.addOverlay(options);
//                实例化一个地理编码查询对象
                GeoCoder geoCoder = GeoCoder.newInstance();
//                设置发地理编码位置坐标
                ReverseGeoCodeOption op = new ReverseGeoCodeOption();
                op.location(latLng);
//                发起反地理编码请求 （经纬度-》地址信息）
                geoCoder.reverseGeoCode(op);
                geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
                    @Override
                    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

                    }

                    @Override
                    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
                        //                        获取点击的坐标
                        String address = reverseGeoCodeResult.getAddress();
                        System.out.println("address=" + address);
                    }
                });

            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                LatLng latLng = mapPoi.getPosition();
                double latitude = latLng.latitude;
                double longitude = latLng.longitude;
                System.out.println("latitude=" + latitude + ",longitude=" + longitude);

//                清楚图层
                map.clear();
//                定义标记坐标点
                LatLng point = new LatLng(latitude, longitude);
//                构建标记点配置项，用于在地图上添加标记
                View markerView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.marker, null);
                TextView markerTitle = (TextView) markerView.findViewById(R.id.markerTitle);
                markerTitle.setText(markerCount + "");
                markerCount++;
                BitmapDescriptor markIcon1 = BitmapDescriptorFactory.fromView(markerView);
                MarkerOptions options = new MarkerOptions().position(point).icon(markIcon1);
//                在地图上添加标记，并且显示
                Overlay overlay = map.addOverlay(options);
//                实例化一个地理编码查询对象
                GeoCoder geoCoder = GeoCoder.newInstance();
//                设置发地理编码位置坐标
                ReverseGeoCodeOption op = new ReverseGeoCodeOption();
                op.location(latLng);
//                发起反地理编码请求 （经纬度-》地址信息）
                geoCoder.reverseGeoCode(op);
                geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
                    @Override
                    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

                    }

                    @Override
                    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
                        //                        获取点击的坐标
                        String address = reverseGeoCodeResult.getAddress();
                        System.out.println("address=" + address);
                    }
                });
                return false;
            }
        });
    }


}
